/*
 *  $Id$
 *  Copyright [2010] [Panxiaobo] 
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  you may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 *  Unless required by applicable law or agreed to in writing, software 
 *  distributed under the License is distributed on an "AS IS" BASIS, 
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 *  See the License for the specific language governing permissions and 
 *  limitations under the License. 
 */
package pxb.openid;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet相关的工具类
 * 
 * @author Panxiaobo
 * 
 */
public class ServletUtil {
    public static void formRedirect(Forward forward, HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter w = resp.getWriter();
        w.append("<html>");
        w.append("<head><title>Redirecting...</title>").append("<meta http-equiv=\"Pragma\" content=\"no-cache\"/>")
                .append("<meta http-equiv=\"Cache-Control\" content=\"no-cache,no-store\"/></head>");
        w.append("<body onload=\"document.forms['auth'].submit();\">").append("Please wait,Redirecting...")
                .append("<form id=\"auth\" name=\"auth\" method=\"POST\" accept-charset=\"utf-8\"")
                .append(" action=\"").append(forward.getServer()).append("\">");
        for (Map.Entry<String, String> entry : forward.getParameters().entrySet()) {
            w.append("<input type=\"hidden\" name=\"").append(entry.getKey()).append("\" value=\"")
                    .append(entry.getValue()).append("\"/>");
        }
        w.append("</form></body>");
        w.append("</html>");
        w.flush();
        w.close();
    }

    /**
     * 获取请求参数
     * 
     * @param req
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> getRequestArgs(HttpServletRequest req) {
        Enumeration<String> e = req.getParameterNames();
        Map<String, String> values = new HashMap<String, String>();
        while (e.hasMoreElements()) {
            String name = e.nextElement();
            String value = req.getParameter(name);
            values.put(name, value);
        }
        return values;
    }

    /**
     * 获取特定前缀的请求参数
     * 
     * @param req
     * @param prefix
     *            特定前缀
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map<String, String> getRequestArgs(HttpServletRequest req, String prefix) {
        Enumeration<String> e = req.getParameterNames();
        Map<String, String> values = new HashMap<String, String>();
        while (e.hasMoreElements()) {
            String name = e.nextElement();
            if (name.startsWith(prefix)) {
                String value = req.getParameter(name);
                values.put(name, value);
            }
        }
        return values;
    }

    /**
     * http://1asdf.com/asdf.adsf?asdfasd 会返回http://1asdf.com/asdf.adsf
     * 
     * @param req
     * @return
     */
    public static String getRequestUrl(HttpServletRequest req) {
        String ret = req.getRequestURL().toString();
        {
            int i = ret.indexOf('?');
            if (i > 0) {
                ret = ret.substring(0, i);
            }
        }
        return ret;
    }

}
